package ils.web.action;

import com.opensymphony.xwork2.Preparable;
import ils.domain.catalog.model.Book;
import ils.domain.issue.model.Issue;
import ils.domain.issue.model.Reserve;
import ils.services.*;
import ils.util.SpringUtil;
import ils.util.Util;
import ils.web.model.MyIssue;
import ils.web.model.ReservationDetails;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class MyAccountAction extends ActionSupport implements SessionAware, Preparable
{
    private Map<String, Object> session;
    private List<MyIssue> myIssueList;
    private List<ReservationDetails> reserves;
    private IssueService issueService;
    private CatalogService catalogService;
    
    public String execute() throws Exception
    {
        int userId = (Integer)session.get("userId");
        List<Issue> issues = issueService.getAUsersOpenIssues(userId);
        myIssueList = getIssueDetails(issues);
        List<Reserve> reserveList  = issueService.getReservationsForUser(userId);
        reserves = getReservationDetails(reserveList);
        return SUCCESS;
    }

    private List<MyIssue> getIssueDetails(List<Issue> issues)
    {
        if(issues == null || issues.size() == 0) return null;
        List<Integer> bookIds = new ArrayList<>();
        for(Issue issue : issues)
        {
            bookIds.add(issue.getItem().getBookId());
        }
        Map<Integer, Book> books = catalogService.getBooks(bookIds);
        List<MyIssue> myIssueList = new ArrayList<>();
        for(Issue issue : issues)
        {
            MyIssue myIssue = new MyIssue();
            Util.copy(myIssue, issue);
            Book book = books.get(issue.getItem().getBookId());
            myIssue.setBookName(book.getTitle());
            myIssueList.add(myIssue);
        }
        return myIssueList;
    }

    private List<ReservationDetails> getReservationDetails(List<Reserve> reserves)
    {
        if(reserves == null || reserves.size() == 0) return null;
        List<Integer> bookIds = new ArrayList<>();
        for(Reserve reserve : reserves)
        {
            bookIds.add(reserve.getBookId());
        }
        Map<Integer, Book> books = catalogService.getBooks(bookIds);
        List<ReservationDetails> reservationDetailsList = new ArrayList<>();
        for(Reserve reserve : reserves)
        {
            ReservationDetails reservationDetails = new ReservationDetails();
            Util.copy(reservationDetails, reserve);
            Book book = books.get(reserve.getBookId());
            reservationDetails.setBookName(book.getTitle());
            int total = 0;
            if(book.getTotalCount() != null) total = book.getTotalCount();
            reservationDetails.setTotal(total);
            int available = 0;
            if(book.getAvailableCount() != null) available = book.getAvailableCount();
            reservationDetails.setAvailable(available);
            reservationDetailsList.add(reservationDetails);
        }
        return reservationDetailsList;
    }
    
    @Override
    public void setSession(Map<String, Object> arg0)
    {
        this.session = arg0;
    }

    public List<MyIssue> getMyIssueList()
    {
        return myIssueList;
    }

    public void setMyIssueList(List<MyIssue> myIssueList)
    {
        this.myIssueList = myIssueList;
    }

    public void setIssueService(IssueService issueService)
    {
        this.issueService = issueService;
    }

    public List<ReservationDetails> getReserves()
    {
        return reserves;
    }

    public void setReserves(List<ReservationDetails> reserves)
    {
        this.reserves = reserves;
    }

    public void setCatalogService(CatalogService catalogService) {
        this.catalogService = catalogService;
    }

    @Override
    public void prepare() throws Exception {
        issueService = (IssueService)SpringUtil.getApplicationContext().getBean("issueService");
        catalogService = (CatalogService)SpringUtil.getApplicationContext().getBean("catalogService");
    }
}
